package com.maimao.kill.rabbit;

import com.maimao.kill.config.RabbitProperties;
import com.maimao.model.kill.dto.KillDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.AbstractJavaTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author MaoLin Wang
 * @date 2021/4/16 7:51 下午
 */
@Service
@Slf4j
public class RabbitService {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * 秒杀成功发送消息
     *
     * @param killDto 秒杀消息
     */
    public void sendKillSuccessMessage(final KillDto killDto) {
        if (killDto != null && killDto.getUserId() != null && killDto.getKillId() != null && killDto.getUserId() > 0 && killDto.getKillId() > 0) {
            try {
                rabbitTemplate.convertAndSend(RabbitProperties.KILL_SUCCESS_EXCHANGE, RabbitProperties.KILL_SUCCESS_ROUTING_KEY, killDto);
                log.info("kill success send msg {}", killDto);
            } catch (Exception e) {
                log.error("kill success send mq fail. msg:{},\n ex:{}", killDto, e);
            }
        }
    }

    /**
     * 提交订单成功后发送消息到延迟队列
     *
     * @param killDto 秒杀消息
     */
    public void sendKillOrderDelayMessage(final KillDto killDto) {
        if (killDto != null && killDto.getUserId() != null && killDto.getKillId() != null && killDto.getUserId() > 0 && killDto.getKillId() > 0) {
            try {
                rabbitTemplate.convertAndSend(RabbitProperties.KILL_ORDER_SUBMIT_DELAY_EXCHANGE, RabbitProperties.KILL_ORDER_SUBMIT_DELAY_ROUTING_KEY, killDto, new MessagePostProcessor() {
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        MessageProperties properties = message.getMessageProperties();

                        properties.setHeader(AbstractJavaTypeMapper.DEFAULT_CONTENT_CLASSID_FIELD_NAME,Long.class);
                        //动态生成TTL
                        properties.setExpiration(RabbitProperties.PAY_EXPIRE);
                        return message;
                    }
                });
                log.info("submit order success send delay msg {}", killDto);
            } catch (Exception e) {
                log.error("submit order success send delay msg fail. msg:{},\n ex:{}", killDto, e);
            }
        }
    }

    /**
     * 预生成订单保存到redis后 发送生成订单的消息
     *
     * @param killDto 包含已生成的订单id
     */
    public void sendSubmitOrderMessage(final KillDto killDto) {
        if (killDto != null) {
            try {
                rabbitTemplate.convertAndSend(RabbitProperties.KILL_ORDER_SUBMIT_EXCHANGE, RabbitProperties.KILL_ORDER_SUBMIT_ROUTING_KEY, killDto);
                log.info("send kill order msg :{}", killDto);
            } catch (Exception e) {
                log.error("send kill order msg fail. orderId:{},\n ex:{}", killDto.getOrderId(), e);
            }
        }
    }

}
